package com.ruoyi.biz.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.ruoyi.biz.domain.WarnedRecord;
import com.ruoyi.biz.mapper.WarnedRecordMapper;
import com.ruoyi.biz.service.IWarnedRecordService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.WarnedStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-30
 */
@Service
public class WarnedRecordServiceImpl implements IWarnedRecordService
{
    @Resource
    private WarnedRecordMapper warnedRecordMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param warnedId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public WarnedRecord selectWarnedRecordById(Long warnedId)
    {
        return warnedRecordMapper.selectWarnedRecordById(warnedId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param warnedRecord 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<WarnedRecord> selectWarnedRecordList(WarnedRecord warnedRecord)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String currentUsername = user.getUserName();
        warnedRecord.setCurrentUser(currentUsername);
        return warnedRecordMapper.selectWarnedRecordList(warnedRecord);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param warnedRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertWarnedRecord(WarnedRecord warnedRecord)
    {
        warnedRecord.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Long deptId = user.getDeptId();
        String userName = user.getUserName();
        warnedRecord.setDeptId(deptId);
        warnedRecord.setCreateBy(userName);
        // 对warnedRecord的状态值进行转化
        Optional<WarnedStatus> optional = Arrays.stream(WarnedStatus.values()).filter(warnedStatus -> warnedStatus.getCode().equals(warnedRecord.getStatus().toString())).findAny();
        if (optional.isPresent()) { // 如果不为空
            WarnedStatus warnedStatus = optional.get();
            warnedRecord.setStatus(Integer.parseInt(warnedStatus.getCode()));
        }
        return warnedRecordMapper.insertWarnedRecord(warnedRecord);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param warnedRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateWarnedRecord(WarnedRecord warnedRecord)
    {
        warnedRecord.setUpdateTime(DateUtils.getNowDate());
        return warnedRecordMapper.updateWarnedRecord(warnedRecord);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param warnedIds 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteWarnedRecordByIds(Long[] warnedIds)
    {
        return warnedRecordMapper.deleteWarnedRecordByIds(warnedIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param warnedId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteWarnedRecordById(Long warnedId)
    {
        return warnedRecordMapper.deleteWarnedRecordById(warnedId);
    }

    @Override
    public Map<String, Object> findTimeDataCount(Long equipmentId) {
        return null;
    }

    @Override
    public WarnedRecord selectWarnedRecord(Long warnedId) {
        return warnedRecordMapper.selectWarnedRecord(warnedId);
    }

    /**
     *
     * @param type
     * @param sensorName
     * @return
     */
    @Override
    public WarnedRecord findData(Integer type, String sensorName) {
        return warnedRecordMapper.findData(type,sensorName);
    }

    /**
     * 去重根据SensorName，并找出最新的报警
     * @return
     * @param equipmentId
     */
    @Override
    public List<WarnedRecord> findAlarmData(Long equipmentId) {
        return warnedRecordMapper.findAlarmData(equipmentId);
    }

    /**
     * 去重根据名称，并找最新的预警
     * @param equipmentId
     * @return
     */
    @Override
    public List<WarnedRecord> findWarnData(Long equipmentId) {
        return warnedRecordMapper.findWarnData(equipmentId);
    }

    /**
     * 根据传感器报警类型和名称查找记录次数
     * @param sensorName
     * @param type
     * @return
     */
    @Override
    public Integer selectSensorInfoNumsByName(String sensorName, Integer type) {
        return warnedRecordMapper.selectSensorInfoNumsByName(sensorName,type);
    }


}
